<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date  2016-12-10 12:18:59
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Mypage extends Admin_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $userinfo = $this->session->userinfo;
        if(!isset($userinfo))
        {
            redirect('admin/login');
        }
    }

    //加载我的页面视图
    public function index()
    {
        $data['id'] = $_SESSION['userinfo']['id'];
        $data['name'] = $_SESSION['userinfo']['name'];
        $data['email'] = $_SESSION['userinfo']['email'];
        $data['phone'] = $_SESSION['userinfo']['phone'];
        
        $this->load->view('templates/header');
        $this->load->view('templates/menu');
        $this->load->view('mypage',$data);

    }
    //修改我的资料
    public function update()
    {
        $name = $_SESSION['userinfo']['name'];
        $email = $_SESSION['userinfo']['email'];
        $phone = $_SESSION['userinfo']['phone'];
        $user = $this->input->post(array('name','email','phone'),TRUE);
        $password = $this->input->post('password',TRUE);
        $id = $this->input->post('id',TRUE);
        if(!strcmp($user['name'], $name) && !strcmp($user['email'], $email) && !strcmp($user['phone'], $phone) && !$password)
        {
            $this->load->view('templates/header');
            $this->load->view('templates/menu');
            $user['message'] = "没有要修改的数据！";
            $this->load->view('mypage',$user);
        
        }
        else
        {
            if($password)
            {
                $repassword = $this->input->post('repassword',TRUE);
                if(strcmp($password, $repassword) == 0)
                {
                    $user['password'] = md5($password);
                }
                else
                {
                    $this->load->view('templates/header');
                    $this->load->view('templates/menu');                    
                    $user['message'] = "两次密码不一样！！！";
                    $this->load->view('mypage',$user);
                    return FALSE;
                }
            }
            
            if($id)
            {
                $this->load->model('admin_model');
                if($this->admin_model->update_one($user,array('name'=>'id','value'=>$id)))
                {
                    unset($_SESSION['userinfo']);
                    $_SESSION['userinfo'] = $this->admin_model->get_one('id',$id);
                    $this->success('修改成功！','admin/system');
                }
                else
                {
                    $this->load->view('templates/header');
                    $this->load->view('templates/menu');                   
                    $user['message'] = "未知错误,不能修改！！！";
                    $this->load->view('mypage',$user);

                }
            }
        }
    }

}